/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kbbywyclient.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.callback.OnItemClickListener;
import com.kbbywyclient.data.entity.ProductLib;
import com.kbbywyclient.util.ViewUtils;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by FHW on 2017/5/10.
 */
public class ProductLibAdapter extends SwipeMenuAdapter<ProductLibAdapter.DefaultViewHolder> {

    private List<ProductLib> titles;
    private static Context mContext;

    private OnItemClickListener mOnItemClickListener;

    public ProductLibAdapter(List<ProductLib> titles, Context context) {
        this.titles = titles;
        this.mContext = context;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemCount() {
        return titles == null ? 0 : titles.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product_lib, parent, false);
    }

    @Override
    public ProductLibAdapter.DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        DefaultViewHolder viewHolder = new DefaultViewHolder(realContentView);
        viewHolder.mOnItemClickListener = mOnItemClickListener;
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ProductLibAdapter.DefaultViewHolder holder, int position) {
        holder.setData(titles.get(position), position);
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        OnItemClickListener mOnItemClickListener;

        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.tv_state)
        TextView tv_state;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            ButterKnife.bind(this, itemView);
        }

        public void setData(ProductLib productLib, int position) {
            ViewUtils.setText(this.tv_name, productLib.getName());
            ViewUtils.setText(this.tv_state, productLib.getParentUsageName() + ",月利率" + productLib.getRateMin() + "-" + productLib.getRateMax()
                    + ",额度" + productLib.getMinAmount() + "-" + productLib.getMaxAmount() + "万");
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }

}
